home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 March
/
EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso
/
earcd
/
util2
/
fiflb381.lha
/
testfifo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-13
|
3KB
|
166 lines
/*
* TEST.C
*
* TEST [R/W][N] fifo_name
*
* test fifo operation
*
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <libraries/dos.h>
#if defined(_DCC) /* at least the very old DICE 2.06 */
#include <clib/exec_protos.h>
#else
#include <proto/exec.h>
#endif
#include <clib/alib_protos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h> /* read(), write() */
#if defined(__GNUC__)
#include <unistd.h> /* read(), write() */
#endif
#include <libraries/fifo.h>
#include <proto/fifo.h>
typedef struct MsgPort MsgPort;
typedef struct Message Message;
struct Library *FifoBase;
void *Fh;
MsgPort *WaPort;
char IBuf[256];
void DoWait(long);
void myexit(void);
int main(ac, av)
int ac;
char *av[];
{
if (ac != 3) {
fputs("TEST [R/W][N] fifo_name\n", stderr);
exit(1);
}
atexit(myexit);
WaPort = CreatePort(NULL, 0);
if ((FifoBase = OpenLibrary(FIFONAME, 0))) {
long flags = FIFOF_NORMAL;
fputs("fifo.library opened!\n", stderr);
if (av[1][1] == 'N')
flags |= FIFOF_NBIO;
switch(av[1][0]) {
case 'R':
if ((Fh = OpenFifo(av[2], 4096, FIFOF_READ | flags))) {
long i = 0;
char *ptr;
fprintf(stderr, "fifo is %ld bytes\n", BufSizeFifo(Fh));
for (;;) {
long n = ReadFifo(Fh, &ptr, i);
if (n > 64) /* limit amount of data read/loop */
n = 64;
i = n;
if (n < 0) {
fputs("EOF\n", stderr);
break;
}
if (n > 0) {
/*
* just so other windows doesn't freeze while we
* output kilobytes.
*/
write(1, ptr, n);
} else { /* n == 0 */
if (flags & FIFOF_NBIO) {
DoWait(FREQ_RPEND);
} else {
puts("0 bytes avail?");
}
}
#if defined(_DCC)
chkabort(); /* exit if ^C */
#endif
}
}
break;
case 'W':
if ((Fh = OpenFifo(av[2], 4096, FIFOF_WRITE | FIFOF_KEEPIFD | flags))) {
long n;
fprintf(stderr, "fifo is %ld bytes\n", BufSizeFifo(Fh));
while ((n = read(0, IBuf, sizeof(IBuf))) > 0) {
long r;
loop:
r = WriteFifo(Fh, IBuf, n);
if (r != n) {
if (r >= 0 && (flags & FIFOF_NBIO)) {
DoWait(FREQ_WAVAIL);
goto loop;
} else {
fprintf(stderr, "write failed! %ld\n", r);
break;
}
}
}
}
break;
default:
fputs("bad command line", stderr);
break;
}
}
return(0);
}
void
myexit(void)
{
if (Fh) {
CloseFifo(Fh, FIFOF_EOF);
Fh = 0;
}
if (FifoBase) {
CloseLibrary(FifoBase);
FifoBase = 0;
}
if (WaPort) {
DeletePort(WaPort);
WaPort = NULL;
}
}
void
DoWait(req)
long req;
{
Message msg;
long mask = 0;
fputs("WAIT\n", stderr);
msg.mn_ReplyPort = WaPort;
RequestFifo(Fh, &msg, req);
while (msg.mn_Node.ln_Type == NT_MESSAGE) {
mask = Wait(SIGBREAKF_CTRL_C | (1 << WaPort->mp_SigBit));
if (mask & SIGBREAKF_CTRL_C)
RequestFifo(Fh, &msg, FREQ_ABORT);
}
GetMsg(WaPort);
if (mask & SIGBREAKF_CTRL_C)
fputs("WAIT: ABORT\n", stderr);
}